import Foundation

extension UIAlertController {
    
    class func showInputAlert(VC: UIViewController, title:String, content: String, placeHolder: String, limitCount: Int? = -1, textDidChange: ((String) -> Void)? = nil, completeHolder: @escaping (String)->Void) {
        
        let alertVC = UIAlertController(title: title, message: "", preferredStyle: .alert);
        alertVC.addTextField { (textField) in
            textField.font = kSetPingFangRegular(14)
            textField.returnKeyType = .done
            textField.text = content
            textField.attributedPlaceholder = NSAttributedString.init(string:placeHolder, attributes: [NSAttributedString.Key.font: kSetPingFangRegular(12)])
            _ = textField.rx.text.orEmpty.asObservable().subscribe(onNext: {
                
                if let limitNum = limitCount {
                    textField.text = Utils.clipTextFieldTextLength(textField: textField, limitNum: limitNum)
                }
                
                guard let valueChangeHolder = textDidChange else { return }
                valueChangeHolder($0)
            })
    
        }
        
        let cancelAction = UIAlertAction(title: "取消".localized, style: .cancel) { (action) in}
        let conformAction = UIAlertAction(title: "确认".localized, style: .default) { (action) in
            guard alertVC.textFields!.count > 0 else { return }
            completeHolder((alertVC.textFields?.first!.text)!);
        }
        
        alertVC.addAction(cancelAction);
        alertVC.addAction(conformAction);
        
        VC.present(alertVC, animated: true, completion: nil);
    }
    
    class func showAlert(VC: UIViewController = Utils.getTopVC()!, alertTitle: String, alertMessage: String? = "", leftActionTitle: String? = nil, rightActionTitle: String? = nil, leftHolder: (()->Void)? = nil, rightHolder: (()->Void)? = nil) {
        
        let alertVC = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert);
        
        if let leftActionT = leftActionTitle {
            let leftAction = UIAlertAction(title: leftActionT, style: .cancel) { action in
                guard let holder = leftHolder else { return }
                holder()
            }
            alertVC.addAction(leftAction)
        }
        
        if let rightActionT = rightActionTitle {
            let rightAction = UIAlertAction(title: rightActionT, style: .default) { action in
                guard let holder = rightHolder else { return }
                holder()
            }
            alertVC.addAction(rightAction)

        }
        
        VC.present(alertVC, animated: true, completion: nil);
        
    }
    
    class func showActionSheet(_ vc: UIViewController,_ actionTitles: [String] ,_ selectItemHolder: @escaping ((String)->Void)) {
        
        let alertVC = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet);
        actionTitles.forEach { (title) in
            let action = UIAlertAction(title: title, style: .default) { (action) in
                selectItemHolder(title)
            }
            alertVC.addAction(action)
        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        alertVC.addAction(cancelAction)
        vc.present(alertVC, animated: true, completion: nil)
        
    }
    
}

